package org.example.knowledgegraph.controller;

import org.example.knowledgegraph.model.CustomLink;
import org.example.knowledgegraph.model.CustomNode;
import org.example.knowledgegraph.model.Movie;
import org.example.knowledgegraph.model.Person;
import org.example.knowledgegraph.repository.EntityRepository;
import org.example.knowledgegraph.service.KnowledgeGraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:8080")
public class KnowledgeGraphApiController {

    @Autowired
    private KnowledgeGraphService knowledgeGraphService;

    // ... existing code ...
    // ... existing code ...
    @GetMapping("/person/{name}")
    public List<CustomNode> findByName(@PathVariable("name") String name) {
        Person person = knowledgeGraphService.findByName(name);
        List<CustomNode> result = new ArrayList<>();
        if (person != null) {
            result.add(new CustomNode(person.getName(), 1,person));
        }
        return result;
    }
// ... existing code ...

    @GetMapping("/query/{relationship}/{name}")
    public List<CustomNode> getActedByName(@PathVariable("relationship") String rel, @PathVariable("name") String name){
        Person person = knowledgeGraphService.findByName(name);
        List<CustomNode> customNodes = new ArrayList<>();
        Set<Movie> movieSet = knowledgeGraphService.getMovieByRelationship(person,rel);
        for (Movie movie : movieSet) {
            customNodes.add(new CustomNode(movie.getTitle(), 2, movie));

        }
        return customNodes;
    }

    @GetMapping("/all")
    public List<CustomNode> findAll() {
        Iterable<Person> personIterable = knowledgeGraphService.findAll();
        List<CustomNode> customNodes = new ArrayList<>();
        for(Person person : personIterable){
            customNodes.add(new CustomNode(person.getName(), 1, person));
        }
        return customNodes;
    }
// ... existing code ...
}
